package cn.zhl.algorithm._3_并查集.impl;

import cn.zhl.algorithm._3_并查集.UnionFind;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-06-11 19:00
 **/
public class QuickFind extends UnionFind {
	public QuickFind(int capacity){
		super(capacity);
	}
	@Override
	public int union(int v1, int v2) {
		int oldRoot = find(v1);
		int newRoot = find(v2);
		if(oldRoot == newRoot){
			return newRoot;
		}
		for(int i = 0;i<parent.length;i++){
			if(parent[i] == oldRoot){
				parent[i] = newRoot;
			}
		}
		return newRoot;
	}

	@Override
	public int find(int v) {
		rangeCheck(v);
		return parent[v];
	}
}
